package com.ruoyi.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hr.common.constant.StatusConst;
import com.ruoyi.hr.common.utils.BaseThreadLocal;
import com.ruoyi.hr.common.utils.StatusParser;
import com.ruoyi.hr.domain.HrResume;
import com.ruoyi.hr.domain.vo.HrCheckDispatchVo;
import com.ruoyi.hr.service.IHrResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hr.mapper.HrCheckMapper;
import com.ruoyi.hr.domain.HrCheck;
import com.ruoyi.hr.service.IHrCheckService;

/**
 * 审核Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-12
 */
@Service
public class HrCheckServiceImpl implements IHrCheckService
{
	@Autowired
	private HrCheckMapper hrCheckMapper;

	@Autowired
	private IHrResumeService hrResumeService;

	/**
	 * 查询审核
	 *
	 * @param id 审核主键
	 * @return 审核
	 */
	@Override
	public HrCheck selectHrCheckById(Long id)
	{
		return hrCheckMapper.selectHrCheckById(id);
	}

	/**
	 * 查询审核列表
	 *
	 * @param hrCheck 审核
	 * @return 审核
	 */
	@Override
	public List<HrCheck> selectHrCheckList(HrCheck hrCheck)
	{
		return hrCheckMapper.selectHrCheckList(hrCheck);
	}

	/**
	 * 新增审核
	 *
	 * @param hrCheck 审核
	 * @return 结果
	 */
	@Override
	public int insertHrCheck(HrCheck hrCheck)
	{
		hrCheck.setCreateTime(DateUtils.getNowDate());
		return hrCheckMapper.insertHrCheck(hrCheck);
	}

	/**
	 * 修改审核
	 *
	 * @param hrCheck 审核
	 * @return 结果
	 */
	@Override
	public int updateHrCheck(HrCheck hrCheck)
	{
		hrCheck.setUpdateTime(DateUtils.getNowDate());
		return hrCheckMapper.updateHrCheck(hrCheck);
	}

	/**
	 * 批量删除审核
	 *
	 * @param ids 需要删除的审核主键
	 * @return 结果
	 */
	@Override
	public int deleteHrCheckByIds(Long[] ids)
	{
		return hrCheckMapper.deleteHrCheckByIds(ids);
	}

	/**
	 * 删除审核信息
	 *
	 * @param id 审核主键
	 * @return 结果
	 */
	@Override
	public int deleteHrCheckById(Long id)
	{
		return hrCheckMapper.deleteHrCheckById(id);
	}

	/**
	 * 批量插入
	 * @param collect
	 */
	@Override
	public void insertBatch(List<HrCheck> collect) {
		hrCheckMapper.insertBatch(collect);

	}

	@Override
	public void updateHrCheckBatch(List<Long> resumeIds, Integer status, Integer checkType) {
		hrCheckMapper.updateHrCheckBatch(resumeIds,status, BaseThreadLocal.getUserId(), checkType);
	}

	@Override
	public List<HrCheck> selectHrCheckByResumeIds(List<Long> resumeIds) {
		return hrCheckMapper.selectByResumeIds(resumeIds);

	}

	@Override
	public void deleteHrCheckBatch(Long resumeId, Integer type) {
		List<Long> resumeIds = new ArrayList<>();
		resumeIds.add(resumeId);
		hrCheckMapper.deleteHrCheckBatch(resumeIds, type);
	}

	@Override
	public void insertBatchWithUserIds(HrCheckDispatchVo dispatchVo) {
		List<Long> userIds = dispatchVo.getUserIds();
		List<Long> resumeIds = dispatchVo.getResumeIds();
		List<HrCheck> list = new ArrayList<>();
		resumeIds.forEach(item->{
			for (Long userId : userIds) {
				HrCheck hrCheck = new HrCheck();
				hrCheck.setIsCheck(StatusConst.Check.CheckStatus.NOCHECK.getCode());
				hrCheck.setType(StatusConst.Check.TypeStatus.HIRE.getCode());
				hrCheck.setResumeId(item);
				hrCheck.setUid(userId);
				hrCheck.setCreateTime(new Date());
				hrCheck.setUpdateTime(new Date());
				list.add(hrCheck);
			}
		});
		// 同样先删掉然后再insert
		hrCheckMapper.deleteHrCheckBatch(resumeIds, StatusConst.Check.TypeStatus.HIRE.getCode());
		hrCheckMapper.insertBatch(list);

		// 同样的修改简历的状态
		List<HrResume> hrResumes = hrResumeService.selectByIds(resumeIds);
		List<HrResume> collect = hrResumes.stream().map(item -> {
			HrResume resume = new HrResume();
			resume.setId(item.getId());
			StatusParser statusParser = new StatusParser(item.getStatus());
			statusParser.setResumeStatus(statusParser.getResumeStatusCode() + 1);
			resume.setStatus(statusParser.getStrStatus());
			resume.setUpdateTime(new Date());
			return resume;
		}).collect(Collectors.toList());
		hrResumeService.updateStatusBatch(collect);
	}

	@Override
	public Boolean ifCanCheck(List<Long> resumeIds, Integer checkType) {
		Long userId = BaseThreadLocal.getUserId();
		boolean flag = true;
		// TODO 实现审核的判断, 当前操作人员能否审核捏
		for (Long id : resumeIds) {
			// 一个个简历的去看,
			HrCheck hrCheck = new HrCheck();
			hrCheck.setResumeId(id);
			hrCheck.setIsCheck(StatusConst.Check.CheckStatus.NOCHECK.getCode());
			List<HrCheck> checks = hrCheckMapper.selectHrCheckList(hrCheck);
			Set<Long> uids = checks.stream().map(HrCheck::getUid).collect(Collectors.toSet());
			if (!uids.contains(userId)){
				// 没有审核的权限, 只要有一个简历, 没有对这个简历的审核权限, 就给他中断所有的审核, 让其自己重新选择捏
				flag = false;
				break;
			}
		}
		return flag;
	}
}
